import { defineStore } from 'pinia'
import { 
  getSystemConfig, 
  updateSystemConfig, 
  getConfigList, 
  createConfig, 
  updateConfig, 
  deleteConfig 
} from '@/api/config'

export const useConfigStore = defineStore('config', {
  state: () => ({
    systemConfig: {},
    configList: [],
    total: 0,
    loading: false
  }),

  actions: {
    async fetchSystemConfig() {
      try {
        const response = await getSystemConfig()
        this.systemConfig = response.data || {}
        return response.data
      } catch (error) {
        throw error
      }
    },

    async updateSystemConfig(data) {
      try {
        return await updateSystemConfig(data)
      } catch (error) {
        throw error
      }
    },

    async fetchConfigList(params) {
      this.loading = true
      try {
        const response = await getConfigList(params)
        this.configList = response.data.records || []
        this.total = response.data.total || 0
        return response.data
      } catch (error) {
        throw error
      } finally {
        this.loading = false
      }
    },

    async createConfig(data) {
      try {
        return await createConfig(data)
      } catch (error) {
        throw error
      }
    },

    async updateConfig(id, data) {
      try {
        return await updateConfig(id, data)
      } catch (error) {
        throw error
      }
    },

    async deleteConfig(id) {
      try {
        return await deleteConfig(id)
      } catch (error) {
        throw error
      }
    }
  }
})